﻿using Futaba.SBus.Telemetry.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Futaba.SBus.Tests.Telemetry.Configuration
{
	[TestClass]
	public class SensorFactoryTests : BaseTest
	{

		[TestMethod, TestCategory("SBUS2")]
		public void GetKnownSensors_Default()
		{
			var target = new SensorFactory();
			var sensors = target.GetKnownSensors();
			foreach (Sensor s in sensors)
			{
				TestContext.WriteLine("Sensor: {0} Parameters: {1}", s, s.Parameters.Length);
			}

			Assert.AreEqual(6, sensors.Length);
			Assert.IsFalse(sensors.Any(s => s.Parameters == null || s.Parameters.Length == 0));
			foreach (SensorType st in Enum.GetValues(typeof(SensorType)))
			{
				if (st == SensorType.Unknown)
					continue;
				Assert.IsNotNull(sensors.Single(s => s.SensorType == st));
			}
		}

		[TestMethod, TestCategory("SBUS2")]
		public void SaveSensors()
		{
			var target = new SensorFactory();
			var sensors = target.GetKnownSensors();
			var outFile = Path.Combine(TestContext.TestResultsDirectory, "knownSensors.xml");
			//sensors.Single(s => s.SensorType == SensorType.RpmSensor)
			//	.Parameters.First().ParsingFlags = SensorValueParsingFlags.ReverseByteOrder | SensorValueParsingFlags.ZeroAllowed;
			using (var stream = File.OpenWrite(outFile))
			{
				target.Save(sensors, stream);
			}
			Assert.IsTrue(File.Exists(outFile));
			Assert.AreNotEqual(0, new FileInfo(outFile).Length);
			TestContext.AddResultFile(outFile);
		}

	}
}
